/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useState, useEffect } from 'react';
import { Form, message, Modal } from 'antd';
import { vipsmsTemplateApproval, getTemplateAuditList } from '@/services/templateAudit';
import type { ApproveDataType, CommonIndexType } from '../types/Common';
import TemCommonCard from './common/TemCommonCard';
import TemRejectModal from './common/TemRejectModal';
import TemCommonForm from './common/TemCommonForm';
import { formatTemSvipApproveParams, getRanString, modalColumns } from './utils';

// apply2approved - 从待批复到通过
// apply2reject - 从待批复到拒绝
// reject2approved - 从已拒绝到通过
// approved2reject - 从已通过到拒绝

const TemAudit = (props: CommonIndexType) => {
  const { screenSize, pageKey, show } = props;
  const [form] = Form.useForm();
  const [dataList, setDataList] = useState([]);
  const [pagination, setPagination] = useState({});
  const [resetSign, setResetSign] = useState('');
  const [modalProps, setModalProps] = useState({});
  const svipStatus = ['12', '13', '14'].includes(pageKey);

  // 获取列表
  const onFinish = async ({ current }: any) => {
    const formValues = form.getFieldsValue();
    const { dataList: list, pagination: paginationParams } = await getTemplateAuditList({
      page_num: current || 1,
      part: pageKey,
      ...formValues,
      state: 'indeterminate',
    });
    setDataList(list.map((item: ApproveDataType) => ({ ...item, ssid: getRanString() })));
    setPagination({ ...paginationParams });
    setResetSign(Date.now().toString());
  };

  useEffect(() => {
    if (show) {
      onFinish({});
    }
  }, [show]);

  // 通过 / 拒绝请求接口
  const onOperationApprove = async (params: any) => {
    const { result } = await vipsmsTemplateApproval(params);
    if (result) {
      message.info('操作成功');
      onFinish(pagination);
    }
  };

  //通过
  const onApprove = (data: ApproveDataType[]) => {
    const params = {
      state: 'approved',
      ...formatTemSvipApproveParams(data),
    };
    if (data.length > 1) {
      Modal.confirm({
        centered: true,
        content: `确认此${data.length}条数据批量通过？`,
        onOk: () => onOperationApprove(params),
      });
      return;
    }
    onOperationApprove(params);
  };
  // 拒绝
  const onRejact = (data: ApproveDataType) => {
    setModalProps({
      visible: true,
      ids: data,
      svipStatus,
      mastRadio: false,
      title: '请选择拒绝原因',
    });
  };

  const onModalClose = () => {
    setModalProps({ visible: false });
  };

  const onModalHandele = (params: any) => {
    const { ids, reason, inputValue } = params;
    const reject: any = modalColumns(svipStatus).filter((item) => item.value === reason);
    const queryData: any = {
      state: 'reject',
      ...formatTemSvipApproveParams([ids]),
    };
    if (inputValue) {
      queryData.rejectwhy = inputValue;
    } else if (reject.length) {
      queryData.rejectwhy = reject[0].value;
    } else {
      message.error('请选择拒绝原因');
      return;
    }
    onOperationApprove(queryData);
    onModalClose();
  };
  return (
    <>
      <TemCommonForm form={form} screenSize={screenSize} onFinish={onFinish} />
      <TemCommonCard
        dataSource={dataList}
        onApprove={onApprove}
        onRejact={onRejact}
        resetSign={resetSign}
        onPageChanges={onFinish}
        svipStatus={svipStatus}
        pagination={{
          ...pagination,
          pageSize: 10,
        }}
      />
      <TemRejectModal onCancel={onModalClose} onok={onModalHandele} {...modalProps} />
    </>
  );
};

export default TemAudit;
